package com.tiancheng.onecode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.onecode.entity.MerchantPayConfig;
import com.tiancheng.onecode.enums.MerchantRegStatusEnums;
import com.tiancheng.onecode.enums.MerchantStatusEnums;
import com.tiancheng.onecode.mapper.MerchantPayConfigMapper;
import com.tiancheng.onecode.model.dto.MerchantTradeUpdateDTO;
import com.tiancheng.onecode.service.IMerchantPayConfigService;
import org.springframework.stereotype.Service;

/**
 * @Author: kellen
 * @Description:商户支付配置管理 service impl
 * @Date: create in 2024/12/9 18:22
 */
@Service
public class MerchantPayConfigServiceImpl extends ServiceImpl<MerchantPayConfigMapper, MerchantPayConfig> implements IMerchantPayConfigService {

    @Override
    public MerchantPayConfig getByMerchantCodeAndPlatformPayChannelIdl(String merchantCode, Long platformPayChannelId) {
        return this.getOne(new LambdaQueryWrapper<MerchantPayConfig>().eq(MerchantPayConfig::getMerchantCode, merchantCode)
                .eq(MerchantPayConfig::getPlatformPayChannelId, platformPayChannelId));
    }

    @Override
    public void setTradeInfo(MerchantTradeUpdateDTO tradeUpdateDTO) {
        final MerchantPayConfig merchantPayConfig = getByMerchantCodeAndPlatformPayChannelIdl(tradeUpdateDTO.getMerchantCode(), tradeUpdateDTO.getPlatformPayChannelId());
        if (null != merchantPayConfig) {
            merchantPayConfig.setTradeMerchantCode(tradeUpdateDTO.getTradeMerchantCode());
            merchantPayConfig.setTradeMerchantPayRegId(tradeUpdateDTO.getTradeMerchantPayRegId());
            merchantPayConfig.setRegCurrentFlows(tradeUpdateDTO.getRegCurrentFlows());
            merchantPayConfig.setMerchantStatus(MerchantStatusEnums.bank_material_supplement.getCode());
            if (tradeUpdateDTO.getRegNextFlows().equals(MerchantRegStatusEnums.CORPORATE_ACCOUNT_VERIFICATION.getCode())) {
                // 下个节点——对公打款验证
                merchantPayConfig.setMerchantRegStatus(MerchantRegStatusEnums.CORPORATE_ACCOUNT_VERIFICATION.getCode());
            } else if (tradeUpdateDTO.getRegNextFlows().equals(MerchantRegStatusEnums.SMS_VERIFICATION.getCode())) {
                // 下个节点——验证码验证申请
                merchantPayConfig.setMerchantRegStatus(MerchantRegStatusEnums.SMS_VERIFICATION.getCode());
            }else if (tradeUpdateDTO.getRegNextFlows().equals(MerchantRegStatusEnums.SIGNING_IN_PROGRESS.getCode())) {
                // 下个节点——前台签约
                merchantPayConfig.setMerchantRegStatus(MerchantRegStatusEnums.SIGNING_IN_PROGRESS.getCode());
            }
            merchantPayConfig.setRegNextFlows(tradeUpdateDTO.getRegNextFlows());
            this.updateById(merchantPayConfig);
        }
    }
}
